/**
 * 弹出窗口
 * @module Window
 * @author pmkx2 (185832959@qq.com)
 */
define('Window', ['jquery'], function($){

    var Window = function(config){
        this.config = $.extend({
            title: null,
            content: null,
            width: 800,
            height: 440,
            zIndex: 999
        }, config || {});

        this.$bg = $('<div class="window-pop-bg"></div>');
        this.$win = $(window);
        this.$el = null;
        this.html = [
            '<div class="window-pop">',
                '<div class="win-title">',
                    '<span class="title-name"></span>',
                    '<a href="javascript:void(0);" class="win-close"><i class="com-icon icon-close-grey inblock"></i>关闭</a>',
                '</div>',
                '<div class="win-content">',
                '</div>',
            '</div>'
            ].join('');
    };

    Window.prototype.open = function(){
        var self = this;
        self._isShow = true;
        self.$bg.show();
        self.$el.css({
            top: (self.$win.height() - self.$el.height()) / 2,
            left: (self.$win.width() - self.$el.width()) / 2
        });
        self.$el.show();
    };

    Window.prototype.close = function(){
        var self = this;
        self._isShow = false;
        self.$bg.hide();
        self.$el.hide();
    };

    Window.prototype.setTitel = function(title){
        var self = this;
        self.$Title = self.$el.find('.title-name');
        if(null === title){
            return false;
        } else if(typeof title === 'string'){
            self.$Title.html(title);
        } else if(typeof title === 'object' && title.appendTo){
            title.appendTo(self.$Title);
        }
    };

    Window.prototype.setContent = function(content){
        var self = this;
        self.$Content = self.$el.find('.win-content');
        if(null === content){
            return false;
        } else if(typeof content === 'string'){
           self.$Content.html(content);
        } else if(typeof content === 'object' && content.appendTo){
            content.appendTo(self.$Content);
        }
    };

    Window.prototype.reSize = function(content){
        var self = this;
        if(self._isShow){
            self.$el.css({
                top: (self.$win.height() - self.$el.height()) / 2,
                left: (self.$win.width() - self.$el.width()) / 2
            });
        }
    };

    Window.prototype.build = function(){
        var self = this;
        var $body = $('body');

        //-- run ----------------------
        self.$el = $(self.html);

        //设置标题
        self.setTitel(self.config.title);
        //设置内容
        self.setContent(self.config.content);
        //预设隐藏
        self.close();

        //-- set ----------------------
        self.$win.on('resize', function(){
            if(self._isShow){
                self.$el.css({
                    top: (self.$win.height() - self.$el.height()) / 2,
                    left: (self.$win.width() - self.$el.width()) / 2
                });
            }
        });

        self.$el.on('click','.win-close',function(){
            self.close();
        });
        /*
        $close.click(function(){
            self.close();
        });
        */

        /*
         this.$content = $content;
        this.$el = $el.hide();
        this.$el.appendTo($body);
        */
       self.$bg.css('z-index',self.config.zIndex).appendTo($body);
       self.$el.css('z-index',self.config.zIndex+1).appendTo($body);
    };

    return Window;
});